export default {
  /**
   *
   * @param {int} timestamp 要转换的时间戳
   * @param {String} fstr 要转化的格式
   * @param {Boolean} utc 是否使用国际时间
   */
  dateFormat(timestamp, fstr, utc) {
    utc = utc ? "getUTC" : "get";
    var date = new Date(timestamp * 1000);
    return fstr.replace(/%[YmdHMS]/g, function(m) {
      switch (m) {
        case "%Y":
          return date[utc + "FullYear"]();
        case "%m":
          m = 1 + date[utc + "Month"]();
          break;
        case "%d":
          m = date[utc + "Date"]();
          break;
        case "%H":
          m = date[utc + "Hours"]();
          break;
        case "%M":
          m = date[utc + "Minutes"]();
          break;
        case "%S":
          m = date[utc + "Seconds"]();
          break;
        default:
          return m.slice(1);
      }
      return ("0" + m).slice(-2);
    });
  },
  dateToTimestamp(date) {
    var time = new Date(date).getTime();
    return time;
  },
  timeshow(timestamp) {
    var timeDate = new Date(timestamp * 1000);
    var nowDate = new Date();
    if (nowDate.getUTCFullYear() - timeDate.getUTCFullYear() > 0) {
      return this.dateFormat(timestamp, "%Y-%m-%d");
    }
    if (nowDate.getUTCMonth() - timeDate.getUTCMonth() > 0) {
      return this.dateFormat(timestamp, "%m-%d");
    }
    if (nowDate.getUTCDay() - timeDate.getUTCDay() > 0) {
      return this.dateFormat(timestamp, "%m-%d");
    }
    if (nowDate.getUTCMinutes() - timeDate.getUTCMinutes() < 5) {
      return "刚刚";
    } else {
      return this.dateFormat(timestamp, "%H:%M");
    }
  },
};
